<?php

/** 
 * 喝水记录统计模型(mongo)
 * @property User_order_mdl $order
 */
class Tongji_mongo_mdl extends Mongo_Model {
	public function __construct() {
		$this->collName = 'tongji_dev';
		parent::__construct ();
	}
	
	public function getDevTongji($fields=array(),$where=array(),$sort=array(),$sortKey){
		$lists = $this->coll->find($where,$fields)
							->sort($sort);
// 							->skip($offset)
// 							->limit($limit);
		$lists =  $this->toArray($lists);
		
		$rst = array();
		
		if( !empty($lists) ){
			foreach ($lists as $k=>$v){
				$rst[$v['did']]['ctime'] = $where['ctime']['$gte'];
				if ( isset($where['ctime']['$lte']) ){
					$rst[$v['did']]['ctime'] .= ' - '.$where['ctime']['$lte'];
				}
				$rst[$v['did']]['name'] = $v['name'];
				$rst[$v['did']]['did'] = $v['did'];
				$rst[$v['did']]['sid'] = $v['sid'];
				$rst[$v['did']]['sname'] = $v['sname'];
				$rst[$v['did']]['price_count'] = (int)(isset($rst[$v["did"]]['price_count'])?($rst[$v["did"]]['price_count']+$v['price_count']):$v['price_count']);
				$rst[$v['did']]['usercount'] = (int)(isset($rst[$v["did"]]['usercount'])?($rst[$v["did"]]['usercount']+$v['usercount']):$v['usercount']);
				$rst[$v['did']]['take_sec'] = (int)(isset($rst[$v["did"]]['take_sec'])?($rst[$v["did"]]['take_sec']+$v['take_sec']):$v['take_sec']);
				$rst[$v['did']]['t_1'] = (int)(isset($rst[$v["did"]]['t_1'])?($rst[$v["did"]]['t_1']+$v['t_1']):$v['t_1']);
				$rst[$v['did']]['t_2'] = (int)(isset($rst[$v["did"]]['t_2'])?($rst[$v["did"]]['t_2']+$v['t_2']):$v['t_2']);
				$rst[$v['did']]['t_3'] = (int)(isset($rst[$v["did"]]['t_3'])?($rst[$v["did"]]['t_3']+$v['t_3']):$v['t_3']);
			}
			
			//排序
			$rst = $this->array_paixu($rst,$sortKey);	
			
		}
		return $rst;
	}
	
	public function array_paixu($list_scene,$key='price_count',$desc='desc'){
		if($desc=='desc'){
			array_multisort(array_column($list_scene,$key),SORT_DESC,$list_scene);
		}else{
			array_multisort(array_column($list_scene,$key),SORT_ASC,$list_scene);
		}
		return $list_scene;
	}
	
	public function statByDaily($query=array()){
		$query['date']=array('$exists'=>TRUE);
		$match = array('$match'=>$query);
		$group = array('$group'=>array(
				'_id'=>'$date',
				'totalAmount'=>array('$sum'=>'$amount'),
				'times'=>array('$sum'=>1),
		));
		$res = $this->coll->aggregate($match,$group);
		
		if( empty($res['ok']) && $res['ok']!=1 && empty($res['result']) ){
			return array();
		}
		$list = $res['result'] ;
		$list = array_combine(array_column($list, '_id'), $list);
		ksort($list);
		return $list;
		
		/* 测试代码:
		$t1 = microtime(true);
		$res = $this->coll->aggregate($match,$group);
		echo microtime(true)-$t1,'<BR>';
		
		$t1 = microtime(true);
		$list = $this->find($query);
		$stat = array();
		foreach ($list as $v){
			$date = date('Y-m-d',$v['createTime']);
			if( empty($stat[$date]) ){
				$stat[$date] = array(
						'totalAmount'=>$v['amount'],
						'times'=>1
				);
			}else{
				$stat[$date]['times'] += 1;
				$stat[$date]['totalAmount'] += $v['amount'];
			}
		}
		echo microtime(true)-$t1,'<BR>';
		
		var_dump($res,$stat);
		 */
	}
	
	public function addDate(){
		$day = strtotime('2016-12-01');
		$end   = time();
		while ($day<$end){
			$where = array();
			$where['date'] = array('$exists'=>false);
			$where['createTime']['$gte'] = $day;
			$day+=86400;
			$where['createTime']['$lt'] = $day;
			$list = $this->coll->find($where);
			foreach ($list as $v){
				$v['date'] = date('Y-m-d',$v['createTime']);
				$this->coll->save($v);
			}
			echo date('Y-m-d H:i:s',$day),'====',count($list),'<br>';
			$list = null;
			
		}
		
		
	}
	
	
// 	/**
// 	 * 保存一张订单
// 	 * @param array $order
// 	 * @return mixed
// 	 */
// 	public function saveOrder($order){
// 		$doc = array ();
// 		foreach ( $order as $k => $v ) {
// 			switch ($k) {
// 				case 'id' :
// 					$doc['_id'] = (float)$v;
// 					break;
// 				case 'devID' :
// 				case 'userID' :
// 				case 'isFinish' :
// 				case 'type' :
// 				case 'takeSec' :
// 				case 'price' :
// 				case 'amount' :
// 					$doc [$k] = ( int ) $v;
// 					break;
// 				case 'createTime' :
// 					$doc [$k] = ( float ) $v;
// 					break;
// 			}
// 		}
// 		return $this->save($doc);
// 	}
// 	/**
// 	 * 保存订单列表
// 	 * @param list $orders
// 	 */
// 	public function saveOrders($orders){
// 		foreach($orders as $order){
// 			$this->saveOrder($order);
// 		}
// 	}
// 	/**
// 	 * 从mysql同步订单到mongo
// 	 */
// 	public function sync (){
// 		$this->load->model('Order_mdl','order');
// 		$this->syncUnfinishOrders();
// 		$maxID = $this->getMaxID();
// 		$limit = 1000; 
// 		$where = array();
// 		$list = array();
// 		do{
// 			$where['id > '] = $maxID;
// 			$list = $this->order->getList($this->select,$where,$limit);
// 			$listCount = count($list);
// 			if( $listCount === 0 ){
// 				break;
// 			}
// 			$maxID = (int)$list[$listCount-1]['id'];
// 			$this->saveOrders($list);
// 		}while($listCount>=$limit);
		
// 		var_dump($this->order->db->queries);
// 	}
	
// 	public function getMaxID(){
// 		$res = $this->coll->find()->sort(array('_id'=>-1))->limit(1)->fields(array('_id'=>true))->getNext();
// 		return empty($res['_id']) ? 0 : (int)$res['_id'];
// 	}
// 	/**
// 	 * 同步未完成的订单
// 	 * @return boolean
// 	 */
// 	private function syncUnfinishOrders(){
// 		$where = array('isFinish'=>0);
// 		$fields = array('_id');
// 		$list = $this->find($where,$fields);
// 		if( empty($list) ){
// 			return TRUE;
// 		}
// 		$list = array_column($list, '_id');
// 		$list = $this->order->getListByFieldList($this->select, 'id', $list);
// 		$this->saveOrders($list);
// 	}
	
	

	
// 	public function generateOrderID(){
// 		static $coll=NULL;
// 		if( NULL===$coll ){
// 			$coll = $this->mongo->selectCollection($this->dbName,'system');
// 		}
// 		$docID = 'orderMaxId';
// 		$key   = 'id';
		
// 		$where = array('_id'=>$docID);
// 		$update = array('$inc'=>array($key=>1));
// 		$res = $coll->findAndModify ($where,$update);
// 		if( !empty($res[$key]) && $res[$key] > 1 ){
// 			return $res[$key];
// 		}
		
// 		$this->load->model('User_order_mdl','order');
// 		$maxID = $this->order->getMaxID();
// 		$maxID+=1;
// 		$doc = array('_id'=>$docID,$key=>$maxID);
// 		$coll->save($doc);
// 		return $maxID;
// 	}
	
	
	
	
	
	
}

